package com.davidhigenbottam.HeatMap.server;

import java.util.HashMap;

import com.davidhigenbottam.HeatMap.data.InstitutionData;
import com.davidhigenbottam.HeatMap.data.ProvinceData;
import com.davidhigenbottam.HeatMap.shared.Query;
import com.davidhigenbottam.HeatMap.shared.ServerResults;

public class DataCache {
	private static DataCache instance = null;
	
	private HashMap<String, ServerResults> dataCache;
	

	
	private DataCache(){
		dataCache = new HashMap<String, ServerResults>();
	}
	
	public static DataCache getInstance() {
		if (instance == null) {
			instance = new DataCache();
		}
		return instance;
	}
	
	
	public void add(String key, ServerResults r){
		dataCache.put(key, r);
	}
	
	
	
	public boolean hasData(String key){
		return dataCache.containsKey(key);
	}
	
	public ServerResults getDataFromCache(String key){
		return dataCache.get(key);
	}
	
	
	
	public ServerResults getData(Query q){
		ServerResults sr;
		String qs= q.getQueryString();
		if(this.hasData(qs)){
			sr= this.getDataFromCache(qs);
		}
		else{
			ProvinceData pd= DataFactory.createProvinceData(q);
			InstitutionData id = DataFactory.createInstitutionData(q);
			
			sr= new ServerResults(pd,id);
			this.add(qs, sr);
		}
		return sr;
	}
	
	
	
}
